package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class WxAuthServiceImpl implements WxAuthService{

    @Autowired
    MarketUserMapper userMapper;


    @Deprecated
    @Override
    public MarketUser login(String username, String password) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(username).andPasswordEqualTo(password);
//        PageHelper.startPage(1,20);
//        //marketUsers类型是Page类型
//        List<MarketUser> marketUsers = userMapper.selectByExample();
//
//        // marketUsers-----vo----oklist
//        ArrayList<Object> vos = new ArrayList<>();
//
//        ResponseUtil.okList(vos, marketUsers);
        MarketUser user = userMapper.selectOneByExample(marketUserExample);
        return user;
    }


    @Override
    public MarketUser findUser(String username) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        MarketUser user = userMapper.selectOneByExample(marketUserExample);
        return user;

    }

    @Override
    public int regCaptcha(MarketUser marketUser,Object mobile, String number) {
        if (marketUser==null){
            //将获取验证码新增至数据库
            marketUser = new MarketUser();
            marketUser.setAvatar("https://web-yxsh.oss-cn-beijing.aliyuncs.com/5b7a3572-1e1f-488e-b8ec-4cc445e8d423.png");
            marketUser.setMobile((String) mobile);
            marketUser.setUsername("      ");
            marketUser.setPassword("      ");
            marketUser.setGender((byte) 0);
            marketUser.setLastLoginIp("1.0.0.0.0");
            marketUser.setWeixinOpenid("");
            marketUser.setLastLoginTime(LocalDateTime.now());
            //注册成功前存储验证码
            marketUser.setNickname(number);
            marketUser.setUserLevel((byte) 0);
            marketUser.setSessionKey("");
            marketUser.setStatus((byte) 0);
            marketUser.setDeleted(false);
            return userMapper.insertSelective(marketUser);
        }else {
            //之前有需要更新验证码及有效期
            marketUser.setMobile((String) mobile);
            marketUser.setLastLoginTime(LocalDateTime.now());
            //注册成功前存储验证码
            marketUser.setNickname(number);
            return userMapper.updateByPrimaryKeySelective(marketUser);
        }



    }

    @Override
    public MarketUser findAlreadyMobile(Object mobile) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andMobileEqualTo((String) mobile);
        MarketUser marketUser1 = userMapper.selectOneByExample(marketUserExample);
        return marketUser1;
    }

    @Override
    public List<MarketUser> findListAlreadyMobile(Object mobile) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andMobileEqualTo((String) mobile);
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        return marketUsers;
    }

    @Override
    public MarketUser register(Map<String, Object> map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //todo
//        try {
//            password = DigestUtils.md5WithSalt(password, "65536");
//        } catch (UnsupportedEncodingException e) {
//            throw new RuntimeException(e);
//        }
        String mobile = (String) map.get("mobile");
        String wxCode = (String) map.get("wxCode");//sessionId

        MarketUser marketUser = new MarketUser();
        marketUser.setUsername(username);
        marketUser.setPassword(password);
        marketUser.setGender((byte) 0);
        marketUser.setSessionKey(wxCode);
        marketUser.setAddTime(LocalDateTime.now());
        marketUser.setLastLoginIp("");
        marketUser.setUserLevel((byte) 0);
        marketUser.setNickname(username);
        marketUser.setMobile(mobile);
        marketUser.setAvatar("https://web-yxsh.oss-cn-beijing.aliyuncs.com/5b7a3572-1e1f-488e-b8ec-4cc445e8d423.png");
        marketUser.setWeixinOpenid("");
        marketUser.setStatus((byte) 0);
        marketUser.setSessionKey("");
        marketUser.setUpdateTime(LocalDateTime.now());
        marketUser.setDeleted(false);
        int i = userMapper.insertSelective(marketUser);

        if (i==1){
            return marketUser;
        }
        return null;
    }


    @Override
    public Integer reset(String tempMobile, String password) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andMobileEqualTo(tempMobile);
        MarketUser marketUser = userMapper.selectOneByExampleSelective(marketUserExample);

        MarketUser marketUser1 = new MarketUser();
        marketUser1.setId(marketUser.getId());
        marketUser1.setPassword(password);
        int i = userMapper.insertSelective(marketUser1);

        return i;
    }

}
